package com.zjd.association.controller;

import com.zjd.association.common.PageWrapper;
import com.zjd.association.common.R;
import com.zjd.association.entity.ApplyLogs;
import com.zjd.association.entity.Users;
import com.zjd.association.handle.CacheHandle;
import com.zjd.association.service.ApplyLogsService;
import com.zjd.association.service.UsersService;
import com.zjd.association.utils.DateUtils;
import com.zjd.association.utils.UUIDUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 申请记录 控制层
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/applyLogs")
public class ApplyLogsController {

    private final CacheHandle cacheHandle;

    private final UsersService usersService;

    private final ApplyLogsService applyLogsService;

    /**
     * 获取路径
     *
     * @return 路径
     */
    @RequestMapping("")
    public String index() {
        return "pages/ApplyLogs";
    }

    /**
     * 获取申请记录信息
     *
     * @param id 申请记录id
     * @return 申请记录
     */
    @GetMapping("/info")
    public R getInfo(String id) {
        ApplyLogs applyLogs = applyLogsService.getOne(id);
        return R.ok(applyLogs);
    }


    /**
     * 获取申请记录信息 分页
     *
     * @param pageIndex 分页页码
     * @param pageSize  分页大小
     * @param token     用户token
     * @param teamName  活动名称
     * @param userName  用户名
     * @return 申请记录
     */
    @GetMapping("/page")
    public R getPageInfos(
            Long pageIndex, Long pageSize,
            String token, String teamName, String userName
    ) {
        //获取用户信息
        Users user = getUser(usersService, cacheHandle, token);
        if (user.getType().equals(0)) {
            PageWrapper page = applyLogsService.getPageInfo(pageIndex, pageSize, null, teamName, userName);
            return R.ok(page);
        } else if (user.getType().equals(1)) {
            PageWrapper page = applyLogsService.getManPageInfo(pageIndex, pageSize, user.getId(), teamName, userName);
            return R.ok(page);
        } else {
            PageWrapper page = applyLogsService.getPageInfo(pageIndex, pageSize, user.getId(), teamName, null);
            return R.ok(page);
        }
    }

    /**
     * 添加申请记录
     *
     * @param token     用户token
     * @param applyLogs 申请记录
     * @return 结果
     */
    @PostMapping("/add")
    public R addInfo(String token, ApplyLogs applyLogs) {
        //获取用户信息
        Users user = getUser(usersService, cacheHandle, token);
        //判断用户是否已经申请过
        if (applyLogsService.isApply(user.getId(), applyLogs.getTeamId())) {
            applyLogs.setId(UUIDUtils.makeIDByCurrent());
            applyLogs.setUserId(user.getId());
            applyLogs.setCreateTime(DateUtils.getNowDate());
            //添加申请记录
            applyLogsService.add(applyLogs);
            return R.ok();
        } else {
            return R.warn("申请审核中，请耐心等待");
        }
    }

    /**
     * 更新用户信息
     *
     * @param applyLogs 申请记录
     * @return 结果
     */
    @PostMapping("/upd")
    public R updInfo(ApplyLogs applyLogs) {
        applyLogsService.update(applyLogs);
        return R.ok();
    }

    /**
     * 删除申请记录
     *
     * @param id 申请记录id
     * @return 结果
     */
    @PostMapping("/del")
    public R delInfo(String id) {
        ApplyLogs applyLogs = applyLogsService.getOne(id);
        applyLogsService.delete(applyLogs);
        return R.ok();
    }

    private Users getUser(UsersService usersService, CacheHandle cacheHandle, String token) {
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        return user;
    }

}